package com.rede.didiok.user.modulelist.rank.restapi;


import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.log.BussinessLog;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.rank.RankCategoryDto;
import com.rede.didiok.common.enums.BehaviorEnum;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.user.modulelist.rank.entity.RankCategoryEntity;
import com.rede.didiok.user.modulelist.rank.service.MemberTagService;
import com.rede.didiok.user.modulelist.rank.service.RankCategoryService;
import com.rede.didiok.user.modulelist.rank.service.RankService;

/**
 * 榜单三级分类表
 *
 * @author Rede
 * @date 2023年11月26日18:59:21
 */
@RestController
//@RequestMapping("/classify")
@RequestMapping("rank/rankCategory")
@Api(value = "分类相关接口", tags = {"分类相关接口"})
@Slf4j
public class RankCategoryRestApi {

    @Autowired
    RankService rankService;
    @Autowired
    MemberTagService memberTagService;
    @Autowired
    RankCategoryService rankCategoryService;


    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/menu/list")
    public R list(){
        List<RankCategoryEntity> entities = rankCategoryService.listWithTree();

        return R.ok().put("data", entities);
    }


    /**
     * 根据分类id查询分类信息
     */
//    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("rank:category:info")
//    public R info(@PathVariable("catId") Long catId){
    @GetMapping("/detail")
    public R info(@RequestParam(value = "catId", required = true) String catId){
        RankCategoryEntity category = rankCategoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/menu/save")
    // @RequiresPermissions("rank:category:save")
    public R save(@RequestBody RankCategoryEntity category){
        rankCategoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/menu/update")
    public R update(@RequestBody RankCategoryEntity category){
        rankCategoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 修改分类
     */
    @RequestMapping("/menu/update/sort")
    public R updateSort(@RequestBody RankCategoryEntity[] category){

        rankCategoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 删除
     * @RequestBody获取请求体，必须发送POST请求
     * SpringMVC自动将请求体的数据(json),转为对应的对象
     */
    @RequestMapping("/menu/delete")
    // @RequiresPermissions("rank:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //删除之前需要判断待删除的菜单那是否被别的地方所引用。
//		categoryService.removeByIds(Arrays.asList(catIds));

        rankCategoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 获取分类的信息
     */
    @ApiOperation(value = "获取分类的信息", notes = "获取分类的信息")
    @GetMapping("/getRankCategoryList")
    public String getRankCategoryList() {
        log.info("获取分类信息");
        return ResultUtil.result(SysConf.SUCCESS, rankCategoryService.getList());
    }

    @BussinessLog(value = "点击分类", behavior = BehaviorEnum.VISIT_CLASSIFY)
    @ApiOperation(value = "通过 catUid 获取榜单", notes = "通过 catUid 获取榜单")
    @GetMapping("/getRankByCatUid")
    public String getRankByCatUid(@ApiParam(name = "catUid", value = "分类UID", required = false) @RequestParam(name = "catUid", required = false) String catUid,
                                          @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                          @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {

        if (StringUtils.isEmpty(catUid)) {
            log.info("点击分类,传入 catUid 不能为空");
            return ResultUtil.errorWithMessage("传入 catUid 不能为空");
        }
        log.info("通过 catUid 获取榜单列表");
        return ResultUtil.result(SysConf.SUCCESS, rankService.getListByCatUid(catUid, currentPage, pageSize));
    }

    /**
     * 获取榜单分类列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取榜单分类列表", notes = "获取榜单分类列表", response = String.class)
    public String list(RankCategoryDto rankCategoryDto) {
        return ResultUtil.successWithData(rankCategoryService.getPageList(rankCategoryDto));
    }

    /**
     * 获取榜单分类详细信息
     */
    @ApiOperation(value = "获取榜单分类详细信息", notes = "获取榜单分类详细信息", response = String.class)
    @GetMapping(value = "/{uid}")
    public String getInfo(@PathVariable("uid") String uid) {
        return ResultUtil.successWithData(rankCategoryService.getById(uid));
    }
}

